package com.zh.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zh.entity.Goods;
import com.zh.service.GoodsService;
import com.zh.util.DateUtil;
import com.zh.vo.Result;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.jms.Destination;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2018/4/11/011.
 */
@RestController
@RequestMapping("/good")
public class GoodsController extends BaseController{

    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/get")
    public Result get(@NotNull(message = "toekn不能为空")
                       @Pattern(regexp = "/^[0-9A-Za-z]{10,}$/",message = "token不能少于10位") String token){
        System.out.println("log:"+token);
        return Result.success(goodsService.selectList(new EntityWrapper<Goods>().eq("delete_mark","0")));
    }

    @RequestMapping("/set")
    public Result set(){
        JSONObject count = goodsService.setGoods();

        JSONArray arr = (JSONArray) count.get("auctions");
        for(Object x: arr){
            Goods goods = new Goods();
            JSONObject ob = ((JSONObject) x);
            goods.setPlace(ob.get("item_loc")+"");
            goods.setStock(ob.get("comment_count")+"");
            goods.setId(ob.get("nid")+"");
            goods.setName(ob.get("raw_title")+"");
            goods.setDescribe(ob.get("view_sales")+"");
            goods.setOwner(ob.get("nick")+"");
            goods.setDeleteMark("0");
            goods.setSpec("S");
            goods.setPrice(ob.get("view_price")+"");
            goods.setCreateTime(DateUtil.getYYYYMMDDH24MISSFormat(new Date()));

            goodsService.insert(goods);

        }
        return Result.success(count);
    }

    @GetMapping(value = "/mq")
    @ResponseBody
    public String activeMqSendMes(@NotNull(message = "内容不能为空")
            String t) {
        try {
            Destination destinationQueue = new ActiveMQQueue("suimh_queue");
            return "activeMQ生产成功！";
        } catch (Exception e) {  
            return "activeMQ生产失败！";
        }
    }
    
    @RequestMapping("/count")
    public JSONObject getCount(String time) throws ParseException {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM");
        Date date =sdf.parse(time);
        cal.setTime(date);
        System.out.print(cal);

        int maxDate = cal.getActualMaximum(Calendar.DATE);
        String[] m = new String[maxDate];
        String[] v = new String[maxDate];

        for(int i = 0;i < maxDate;i++){
            Random rand = new Random();
            m[i] = i+1+"";
            v[i] = rand.nextInt(7) + 1 +"";
        }

        List list = new ArrayList();
        for(int i = 0;i < 4;i++){
            Random rand = new Random();
            JSONObject mm = new JSONObject();
            mm.put("name","value"+i);
            mm.put("value",rand.nextInt(210) + 1 +"");
            list.add(mm);
        }

        JSONObject mmp = new JSONObject();
        mmp.put("month",m);
        mmp.put("value",v);
        mmp.put("type",list);

        return mmp;
    }
}
